#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.H"
#include "AD.h"
#include "Key.h"
#include "MyRTC.h"
#include <stdio.h>  
#include <math.h>

uint8_t KeyNum;

void Buzzer_Init(void)
{
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
		GPIO_InitTypeDef GPIO_InitStructure;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
		GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
		GPIO_Init(GPIOB,&GPIO_InitStructure);
	
		GPIO_SetBits(GPIOB,GPIO_Pin_12);

}

void Buzzer_ON(void)
{
	
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}

void Buzzer_OFF(void)
{
	
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
}

int main(void)
{
		int flag=0;
		int R;
		double I=0;
		OLED_Init();
	  MyRTC_Init();
		AD_Init();
		Buzzer_Init();
    Key_Init();
		MyRTC_Init();	
	

//		OLED_ShowString(1,1,"AD0");
//		OLED_ShowString(2,1,"AD1");
		OLED_ShowString(3,1,"Lux:");
		OLED_ShowString(4,1,"Model:");
	
		while(1)
		{
			MyRTC_ReadTime();
			KeyNum =Key_GetNum();
			R =10240000/(1.1*AD_Value[2])-2500;
			I = (pow(R/10 / (4 * exp(1) + 0.4), -1.0 / -0.6021));
			if(KeyNum==1)
				flag++;
//		OLED_ShowNum(1,5,AD_Value[2],7);
//		OLED_ShowNum(2,5,R,7);
			OLED_ShowString(1, 1, "Date:XXXX-XX-XX");
			OLED_ShowString(2, 1, "Time:XX:XX:XX");
			OLED_ShowNum(1, 6, MyRTC_Time[0], 4);		
			OLED_ShowNum(1, 11, MyRTC_Time[1], 2);	
			OLED_ShowNum(1, 14, MyRTC_Time[2], 2);		
			OLED_ShowNum(2, 6, MyRTC_Time[3], 2);		
			OLED_ShowNum(2, 9, MyRTC_Time[4], 2);		
			OLED_ShowNum(2, 12, MyRTC_Time[5], 2);		
			OLED_ShowNum(4, 7, flag, 1);	
			OLED_ShowNum(3,5,I,4);
				if( (I > 250 | I < 600)|flag==1)
				{
					OLED_ShowNum(3,5,I,4);
				}

				if( (I <= 250 | I >= 600)&&flag!=1)
				{
					OLED_ShowNum(3,5,I,4);
					Buzzer_ON();
					Delay_ms(100);
					Buzzer_OFF();
					Delay_ms(100);

				}
			if(flag>1)
				flag=0;
			Delay_ms(200);
		}
}